import {observable, action, toJS} from 'mobx';

import {message} from 'antd'
import * as API_LIST from '../apis/calView'
import moment from 'moment';
import {WeaTableNew} from 'comsMobx';
const {TableStore} = WeaTableNew;
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
export default class CalView {
	title = `${getLabel(32483,'会议日历')}`;
	@observable userList = [];
	@observable comsWeaTableStore = {};
	@observable status = {
		loading: false,
		type: 1,//1:日历，2:表格
		meetingType: 0,//会议进行状态 0 所有 1已结束 2进行中 3未开始
		nextMeeting: '',
		data: '',
		dataKey: '',
		month: '',
		date: new Date(),//日期
		controlModal: false,
		datas: '',//e9会议-新建会议页面显示内容
		orderFields: '',
		viewMeetingField: '',//查看会议全部内容
		caller: '',
		previewModal: false,
		newmeetingTab: '1',
		createDate: new Date(),
		meetingPerson: '',
		meetingPersonName:'',
		weekStartDay: 0,
		// activeKey:'0',
		viewcondition: '0',
		userRoot:[],
		onlyFlowCreate:0,//是否只允许使用流程新建会议
		hideCreateEntry:0,//是否隐藏新建入口,只能通过流程模块新建会议
		workflowBaseIds:'',//新建会议流程使用的workflow_base对应的id
		requestFieldIds:{},//对应只有一条流程打开时候需要带出参数
		beginTime:'',
		endTime:''
	};
	
	@observable isMeetingUsageBtn = false; //是否会议室使用情况按钮打开

	@observable canCreate = false; //是否允许新建

	@observable isCalendar = false;//为前端无侵入开发（默认视图）做控制
	constructor() {

	}

	@action
	setState = (params) => {
		Object.keys(params).forEach(key => {
			this[key] = params[key];
		});
	}

	setStatus = (params, callback) => {
		Object.keys(params).forEach(key => {
			this.status[key] = params[key];
		});
		typeof callback == 'function' && callback();
	}

	@action
	addUser(params) {
		this.userList.push({ name: "dyf" });
	}

	getUserList(params) {
		return this.userList.slice();
  }
  
  changeCanCreate = (params)=>{
    this.canCreate = params
  }


	// export const doLoading = loading => {
	//     return (dispatch, getState) => {
	//         dispatch({
	//             type: types.CALVIEW_LOADING,
	//             loading
	//         });
	//     }
	// }

	doLoading = (loading) => {
		this.status = { ...this.status, loading: loading }
	}

	doInit = () =>{
		let value = {
			selectUser: '',
			selectdate: moment().format("YYYY-MM-DD"),//拼接日期格式为YYYY-MM-dd
			meetingType: 0,
		}
		const { type } =this.status

		API_LIST.getNextMeetingP({}).then(result=>{
			this.status = {...this.status,
				nextMeeting:result
			}
		})
		if(type==1 || this.isCalendar){
			API_LIST.getDataP(value).then(result=>{
				this.status = {...this.status,
					data:result,
					type: 1,//1:日历，2:表格
					meetingType: 0,//会议进行状态 0 所有 1已结束 2进行中 3未开始
					date:new Date(),//日期
					meetingPerson: '',//''为本人
					// activeKey:'0',
					viewcondition: '0',
					userRoot:[],
					meetingPersonName:'',
					meetingPerson:'',
					onlyFlowCreate:result.onlyFlowCreate || 0,
					hideCreateEntry:result.hideCreateEntry || 0,
					workflowBaseIds:result.workflowBaseIds || '',
					requestFieldIds:result.requestFieldIds || {},
					beginTime:result.beginTime,
					endTime:result.endTime,
					weekStartDay:result.weekStartDay
				}
				
				if(result.hideCreateEntry == 1){
					this.canCreate = false
				}else{
					this.canCreate = true
				}
				//判断是否禁止新建
				window.eventRegister.startList("checkCreateMeeting",function(){},function(){});
			})
		}else{
			type==2 && API_LIST.getCalendarListP(value).then(data=>{
				this.comsWeaTableStore = {...this.comsWeaTableStore, [data.sessionkey]:new TableStore()};
				this.comsWeaTableStore[data.sessionkey].getDatas(data.sessionkey, 1);
				// this.comsWeaTableStore.getDatas(data.sessionkey, 1);
				this.status = { ...this.status, 
					dataKey: data.sessionkey ,
					type: 2
				}
			})
		}
		/*let p1 = API_LIST.getNextMeetingP({})
		let p2 = API_LIST.getDataP(value)
		Promise.all([p1,p2]).then((result)=>{
			this.status = {...this.status,
				nextMeeting:result[0],
				data:result[1],
				type: 1,//1:日历，2:表格
				meetingType: 0,//会议进行状态 0 所有 1已结束 2进行中 3未开始
				date:new Date(),//日期
				meetingPerson: '',//''为本人
				// activeKey:'0',
        		viewcondition: '0',
				userRoot:undefined,
				meetingPersonName:'',
        meetingPerson:'',
        onlyFlowCreate:result[1].onlyFlowCreate || 0,
        hideCreateEntry:result[1].hideCreateEntry || 0,
        workflowBaseIds:result[1].workflowBaseIds || '',
        requestFieldIds:result[1].requestFieldIds || {},
        beginTime:result[1].beginTime,
        endTime:result[1].endTime
      }
      
      if(result[1].hideCreateEntry == 1){
        this.canCreate = false
      }else{
        this.canCreate = true
      }
			//判断是否禁止新建
			window.eventRegister.startList("checkCreateMeeting",function(){},function(){});
		})*/
	}
  
  changeViewcondition = (e)=>{
		this.status = { ...this.status, viewcondition:e}
	}

	//table 等下修改
	//获取table
	// export const getCalendarList = (value = {}) => {
	// 	return (dispatch, getState) => {
	// 		dispatch({ type: types.CALVIEW_LOADING, loading: true });
	// 		API_LIST.getCalendarListP(value).then(data => {
	// 			dispatch(WeaTableAction.getDatas(data.sessionkey, 1));
	// 			dispatch({ type: types.CALVIEW_GETCALENDARLIST, data: data.sessionkey });
	// 			dispatch({ type: types.CALVIEW_LOADING, loading: false });
	// 		});
	// 	}
	// }

	getCalendarList = (value = {})=>{
		const { type } =this.status
		if (type == 2) {
			this.doLoading(true)
			API_LIST.getCalendarListP(value).then(data =>{
	
				this.comsWeaTableStore = {...this.comsWeaTableStore, [data.sessionkey]:new TableStore()};
				this.comsWeaTableStore[data.sessionkey].getDatas(data.sessionkey, 1);
	
				// this.comsWeaTableStore.getDatas(data.sessionkey, 1);
				this.status = { ...this.status, dataKey: data.sessionkey ,loading:false}
				// this.doLoading(false)
			})
		};
  }

	//切换模式
	// export const changeType = value => {
	// 	return (dispatch, getState) => {
	// 		dispatch({
	// 			type: types.CALVIEW_CHANGETYPE, data: value
	// 		});
	// 	}
	// }
	changeType = (value) => {
		if(value==1){
			this.isCalendar=true;
		}else{
			this.isCalendar=false;
		}
		this.status = { ...this.status, type: value }
		this.doUpdate();
	}
	//切换新建会议里的tab
	// export const changeNewMeetingTab = value => {
	// 	return (dispatch, getState) => {
	// 		dispatch({
	// 			type: types.CALVIEW_CHANGENEWMEETINGTAB, data: value
	// 		});
	// 	}
	// }
	changeNewMeetingTab = (value) => {
		this.status = { ...this.status, newmeetingTab: value }
	}


	//切换会议状态
	// export const changeMeetingType = value => {
	// 	return (dispatch, getState) => {
	// 		dispatch({
	// 			type: types.CALVIEW_CHANGEMEETINGTYPE, data: value
	// 		});
	// 	}
	// }
	changeMeetingType = (value) => {
		this.status = { ...this.status, meetingType: value }
		this.doUpdate();
	}


	//切换 会议人员
	// export const changeMeetingPerson = value => {
	// 	return (dispatch, getState) => {
	// 		dispatch({
	// 			type: types.CALVIEW_CHANGEMEETINGPERSON, data: value
	// 		});
	// 	}
	// }
	changeMeetingPerson = (value, name) => {
		this.status = { ...this.status, meetingPerson: value, meetingPersonName:name}
		this.doUpdate();
	}
	changeMeetingUserRoot = (datas, ids, name) => {
			this.status = { ...this.status, userRoot:datas, meetingPerson: ids, meetingPersonName:name}
			this.doUpdate();
	}

	//e9会议日历-获取下一个要参加的会议
	// export const getNextMeeting = (value = {}) => {
	// 	return (dispatch, getState) => {
	// 		API_LIST.getNextMeetingP(value).then(data => {
	// 			dispatch({ type: types.CALVIEW_GETNEXTMEETING, data: data });
	// 		});
	// 	}
	// }
	getNextMeeting = (value = {})=>{
    API_LIST.getNextMeetingP(value).then(result => {
        this.status = {...this.status,nextMeeting:result}
    }).catch(error => {
        message.error(error)
    })
  }

	//e9会议日历-会议日历数据获取
	// export const getData = (value = {}) => {
	// 	return (dispatch, getState) => {
	// 		API_LIST.getDataP(value).then(data => {
	// 			dispatch({ type: types.CALVIEW_GETDATA, data: data });
	// 		});
	// 	}
	// }
	getData = (value = {})=>{
    API_LIST.getDataP(value).then(result => {
        this.status = {...this.status,data:result}
    }).catch(error => {
        message.error(error)
    })
  }


	//切换月份
	// export const changeMonth = value => {
	// 	return (dispatch, getState) => {
	// 		dispatch({
	// 			type: types.CALVIEW_CHANGEMONTH, data: value
	// 		});
	// 	}
	// }
	changeMonth = (value) => {
		this.status = { ...this.status, month: value }
	}

	//保存日期
	// export const saveDate = value => {
	// 	return (dispatch, getState) => {
	// 		dispatch({
	// 			type: types.CALVIEW_SAVEDATE, data: value
	// 		});
	// 	}
	// }

	onlySaveDate = (value) => {
		this.status = { ...this.status, date: value }
	}

	saveDate = (value) => {
		this.status = { ...this.status, date: value }
		this.doUpdate();
	}

	doUpdate = ()=>{
		let {type, meetingPerson, date, meetingType} = this.status;
		if (type == 1 || this.isCalendar) {
			this.getData({
				selectUser: meetingPerson,
				selectdate:  moment(date).format("YYYY-MM-DD"),
				meetingType: meetingType,
			});
		} else {
			let value = {
				selectUser: meetingPerson,
				selectdate:  moment(date).format("YYYY-MM-DD"),
				meetingType: meetingType,
			}
			this.getCalendarList(value);
		}
	}
	// openMeetingDialog = (visible=true,type='preview',meetingId='')=>{
	// 	this.status = { ...this.status, visible: visible, type: type, meetingId: meetingId }
	// }

	/**
	 * 下面的内容未修改 暂时注释
	 */

	// //新建会议显隐
	// export const controlModal = (value, previewMeetingId = '', date = new Date(), newType = 'create') => {
	// 	return (dispatch, getState) => {

	// 		dispatch({ type: types.CALVIEW_CREATEDATE, data: date });
	// 		dispatch({
	// 			type: types.CALVIEW_CONTROLMODAL, data: {
	// 				type: newType,
	// 				previewMeetingId: previewMeetingId,
	// 				control: value,
	// 			}
	// 		});
	// 	}
	// }

	// //表单内容存储到store
	// export const saveFields = (value = {}) => {
	// 	return (dispatch, getState) => {
	// 		dispatch({ type: types.CALVIEW_SAVE_FIELDS, data: value })
	// 	}
	// }



	// //e9会议-会议类型切换获取默认召集人
	// export const changeBroserMeetingType = value => {
	// 	return (dispatch, getState) => {
	// 		API_LIST.changeBroserMeetingTypeP(value).then(data => {

	// 			dispatch(saveFields({ ...value.oldFields, caller: { name: data.name, value: data.id } }));
	// 			dispatch({ type: types.CALVIEW_CHANGEBROSERMEETINGTYPE, data: data });
	// 		});
	// 	}
	// }


	// export const savePreviewMeetingId = (value = {}) => {
	// 	return (dispatch, getState) => {
	// 		dispatch({ type: types.CALVIEW_SAVEPREVIEWMEETINGID, data: value.meetingid });
	// 	}
	// }


	// export const roomPlanVisible = (value = '') => {
	// 	return (dispatch, getState) => {
	// 		dispatch({ type: types.CALVIEW_ROOMPLANVISIBLE, data: value });
	// 	}
	// }

	// //重置页面状态
	// export const resetCalView = (value = '') => {
	// 	return (dispatch, getState) => {
	// 		dispatch({ type: types.CALVIEW_RESETCALVIEW, data: value });
	// 	}
	// }

	// export const setZIndex = (value = '') => {
	// 	return (dispatch, getState) => {
	// 		dispatch({ type: types.CALVIEW_SETZINDEX, data: value });
	// 	}
	// }

}